package com.party.admin.web.controller.photo;

import com.party.admin.biz.photo.AlbumBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.photo.PhotoProject;
import com.party.core.model.photo.PhotosTag;
import com.party.core.service.photo.IPhotoProjectService;
import com.party.core.service.photo.IPhotosTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Description: 相册分类
 * @Author: yangshoukun
 * @Date: 2018/5/30 14:21
 */
@Controller
@RequestMapping("/photo/album")
public class AlbumController {

    @Autowired
    private AlbumBizService albumBizService;

    @Autowired
    private IPhotosTagService photosTagService;

    @Autowired
    private IPhotoProjectService photoProjectService;

    /**
     * 列表
     * @param input
     * @param photosTag
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list(CommonInput input, PhotosTag photosTag, Page page) {
        final String pattern = "yyyy-MM-dd";

        page.setLimit(20);

        ModelAndView mv = new ModelAndView("photo/photosTag");

        Map<String, Object> params = CommonInput.appendParams(input);
        if (null != params.get("startDate")) {
            input.setCreateStart(params.get("startDate").toString().substring(0, pattern.length()));
        }
        if (null != params.get("endDate")) {
            input.setCreateEnd(params.get("endDate").toString().substring(0, pattern.length()));
        }

        List<PhotosTag> list = albumBizService.list(params, photosTag, page);

        PhotoProject photoProject = photoProjectService.get(photosTag.getPhotosId());
        mv.addObject("photoProject", photoProject);
        mv.addObject("input", input);
        mv.addObject("photosTag", photosTag);
        mv.addObject("page", page);
        mv.addObject("list", list);
        return mv;
    }

    /**
     * 添加/编辑页面
     * @param id
     * @return
     */
    @RequestMapping("/addAlbum")
    public ModelAndView addAlbum(String id, String photosId) {
        ModelAndView mv = new ModelAndView("/photo/addAlbum");
        PhotosTag photosTag = new PhotosTag();
        if (StringUtils.isNotEmpty(id)) {
            photosTag = photosTagService.get(id);
        }
        photosTag.setPhotosId(photosId);
        mv.addObject("photosTag", photosTag);
        return mv;
    }

    /**
     * 保存
     * @param photosTag
     * @return
     */
    @ResponseBody
    @RequestMapping("save")
    public AjaxResult save(PhotosTag photosTag) {
        try {
            albumBizService.save(photosTag);
            return AjaxResult.success("保存成功");
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("保存异常");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public AjaxResult delete(String id) {
        try {
            albumBizService.delete(id);
            return AjaxResult.success("删除成功");
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("删除异常");
        }
    }

    /**
     * 添加照片
     * @param photosId
     * @param albumId
     * @return
     */
    @ResponseBody
    @RequestMapping("addPic")
    public AjaxResult addPic(String photosId, String albumId, String urls, String imgResourceIds) {
        try {
            albumBizService.addPic(photosId, albumId, urls, imgResourceIds);
            return AjaxResult.success("添加照片成功");
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("添加照片异常");
        }
    }

    /**
     * 分类列表
     * @param photosId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/tags")
    public AjaxResult tags(String photosId) {
        try {
            List<PhotosTag> outputs = albumBizService.tags(photosId);
            return AjaxResult.success(outputs);
        } catch (BusinessException be) {
            return AjaxResult.error(be.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("获取分类列表异常");
        }
    }
}
